Implementation of Least Recently Used (LRU) page replacement algorithm using Counters

您所在的位置:网站首页 solved page replacement algorithm program code in Implementation of Least Recently Used (LRU) page replacement algorithm using Counters

Implementation of Least Recently Used (LRU) page replacement algorithm using Counters

2023-05-25 17:50| 来源: 网络整理| 查看: 265

Prerequisite – Least Recently Used (LRU) Page Replacement algorithm Least Recently Used page replacement algorithm replaces the page which is not used recently. Implementation: In this article, LRU is implemented using counters, a ctime (i.e., counter) variable is used to represent the current time, it is incremented for every page of the reference array. A vector of pair is used to represent the page frames, the 1’st variable of the pair is the page number and the second variable is the current time. When a new page is to be inserted and the frames are full, the page with minimum ctime is deleted (as it is the least recently used page). If the page is accessed again the value of ctime is updated. Note: Least ctime (counter / current time) value represents the least recently used page. Examples:

Reference array is : 0, 0, 0, 2, 3, 0, 5, 7, 1, 2, 0, 8 Output : When the number of frames is : 3 The number of page faults are : 9 Reference array is : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 0, 0 Output : When the number of frames is : 3 The number of page faults are : 15

Code: 

CPP

#include    using namespace std;   // To calculate the number of page faultsvoid pageFaults(int frame_size, int* ref, int len, int ctime){    // Count variable to count the    // number of page faults    int cnt = 0;    // Arr to simulate frames    vector arr;       // To initialise the array    for (int i = 0; i < frame_size; i++) {        arr.push_back(make_pair(-1, ctime));    }       int page;       for (int i = 0; i < len; i++) {        page = ref[i];        auto it = arr.begin();           for (it = arr.begin(); it != arr.end(); it++) {            if (it->first == page) {                break;            }        }           // If page is found        if (it != arr.end()) {            // update the value of            // current time            it->second = ctime;        }           // If page is not found        else {            // Find the page with min value of ctime,            // as it will be the least recently used            vector::iterator pos;            pos = arr.begin();            int min = pos->second;            for (auto itr = arr.begin(); itr != arr.end(); itr++) {                if (itr->second < min) {                    pos = itr;                    min = pos->second;                }            }               // Erase this page from the frame vector            arr.erase(pos);               // Insert the new page            arr.push_back(make_pair(page, ctime));            cnt++;        }        ctime++;    }    cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3